<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ page isELIgnored="false"%>
<%@page import="com.tueye.utils.TuEyeConstant,java.lang.annotation.*,com.tueye.annotation.*"%>
<%@page import="java.lang.reflect.Field"%>
<%@page import="com.tueye.annotation.ModifyType"%>
<%@page import="org.apache.commons.logging.Log,org.apache.commons.logging.LogFactory"%>
<% 
	String classID = request.getParameter(TuEyeConstant.CLASS_ID);
	String classStr = TuEyeConstant.getClassStrByClasId(classID);
	// classStr model类名 
	classStr = "com.tueye.entity.Admin" ;  // 测试用，可以注释掉
	//反射取得实体类
	Class clazz = Class.forName(classStr);
	//表格的标题
	String title = "";
	//表格的colModel
	StringBuffer colModel = new StringBuffer();
	//表格的button包括添加修改删除
	StringBuffer buttons =	 new StringBuffer() ;
	//表格的搜索项
	StringBuffer searchitems =  new StringBuffer() ;
	//实体上是否有GridClassConfig标注的配置
	boolean titleFlag = clazz.isAnnotationPresent(GridClassConfig.class); 
	/****************************************/
	/*		首先处理类上面的注释配置				*/
	/****************************************/
	if(titleFlag){
		GridClassConfig confStr =(GridClassConfig) clazz.getAnnotation(GridClassConfig.class);
		//取得配置上的title值，作为表格的标题
		title = confStr.showTitle();
		//取得配置的操作类型如：可新增、修改、删除。
		//{name: '增加', bclass: 'add', onpress : exOperation},
		String [] operateType = confStr.operateType();
		buttons.append("["); 
		for(String operate : operateType ){
			if(operate != null && "add".equals(operate)){
				buttons.append("{name :'增加',bclass: 'add',onpress : exOperation},");
			}
			if(operate != null && "delete".equals(operate)){
				buttons.append("{name :'删除',bclass: 'delete',onpress : exOperation},");
			}
			if(operate != null && "edit".equals(operate)){
				buttons.append("{name :'修改',bclass: 'edit',onpress : exOperation},");
			}
		}
		//缺省配置表示 {"add,delete,edit"} 可新增、修改、删除。
		if (operateType == null)
		{
			buttons.append("{name :'增加',bclass: 'add',onpress : exOperation},");
			buttons.append("{name :'删除',bclass: 'delete',onpress : exOperation},");
			buttons.append("{name :'修改',bclass: 'edit',onpress : exOperation},");
		}
		buttons.append("{separator: true}]");
	}
	/****************************************/
	/*		然后处理类属性的注释配置				*/
	/****************************************/
	colModel.append("[ ") ;
	searchitems.append("[ ");
	//循环取得类的父类，并取得类的属性判断属性上的配置封装colModel，直到类无父类为止
	int i = 0 ;
	for(;clazz != null ; clazz = clazz.getSuperclass()){
		for(Field field:clazz.getDeclaredFields()){  
			if(i==0){
				colModel.append("{display: '编号', name : 'numid', width : 40, sortable : true, align: 'center'},");
				i++ ;
			}
			boolean flag = field.isAnnotationPresent(GridFieldConfig.class) ;
			if(flag){
				GridFieldConfig fieldConf = (GridFieldConfig)field.getAnnotation(GridFieldConfig.class) ;
				String fieldAttrName = field.getName();
				//取得修改类型
				ModifyType modifyType = fieldConf.modifytype() ;
				//属性显示的标题（表格各列的名称）
				String name = fieldConf.showName() ;
				//属性字段的长度
				int size = fieldConf.size() ;
				//是否可排序
				boolean sortable =fieldConf.sortable() ; 
				if(sortable){
					searchitems.append("{display: '"+name+"', name : '"+fieldAttrName+"'},") ;
				}
				if("id".equalsIgnoreCase(fieldAttrName)){
					colModel.insert(1,"{display: '" + name +  "', name : '" + fieldAttrName + "', width : " + size + ", sortable : " + sortable + ", align: 'left',hide: true},");
					//colModel.append("{display: '" + name +  "', name : '" + fieldAttrName + "', width : " + size + ", sortable : " + sortable + ", align: 'left',hide: true},");
				}else{
					colModel.append("{display: '" + name +  "', name : '" + fieldAttrName + "', width : " + size + ", sortable : " + sortable + ", align: 'left'},");
				}
			}
		}
	}
	//删除最后一个 ","
	searchitems.deleteCharAt(searchitems.length() - 1) ;
	searchitems.append("]") ;
	//删除最后一个 ","
	colModel.deleteCharAt(colModel.length() - 1) ;
	colModel.append("]") ;
	//如果 searchitems ="[]" ，则置null
	if(searchitems.length() ==2)
		searchitems = null;
	//如果 colModel ="[]"，则置null
	if(colModel.length() ==2)
		colModel = null;
	request.setAttribute("title",title);
	request.setAttribute("colModel",colModel.toString());
	request.setAttribute("buttons",buttons.toString());
	request.setAttribute("searchitems",searchitems.toString());
	
	System.out.println("title\t\t:==:" + title.toString());
	System.out.println("colModel\t:==:" + colModel.toString());
	System.out.println("buttons\t\t:==:" + buttons.toString());
	System.out.println("searchitems\t:==:" + searchitems.toString());
	///global.do?action=PageShow'
			//article.do?
 %>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>tueye广告信息管理-tueye广告管理-tueye专业的图片欣赏交流社区</title>
		
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/themes/flexigrid/css/flexigrid.css" />
		<script type="text/javascript" src="<%=request.getContextPath() %>/script/jquery.js"></script>
		<script type="text/javascript" src="<%=request.getContextPath() %>/script/grid/flexigrid.js"></script>
		<style type="text/css" media="all"> 
			@import "<%=request.getContextPath() %>/themes/flexigrid/css/thickbox.css"; 
		</style>
		<link rel="alternate stylesheet" type="text/css" href="<%=request.getContextPath() %>/themes/flexigrid/css/1024.css" title="1024 x 768" />
		<script src="<%=request.getContextPath() %>/script/grid/thickbox.js" type="text/javascript"></script>
		<style>
			.flexigrid div.fbutton .add {
				background: url(<%=request.getContextPath() %>/themes/flexigrid/images/add.png) no-repeat center left;
			}
			
			.flexigrid div.fbutton .delete {
				background: url(<%=request.getContextPath() %>/themes/flexigrid/images/close.png) no-repeat center left;
			}
			.flexigrid div.fbutton .edit {
				background: url(<%=request.getContextPath() %>/themes/flexigrid/images/plugin.gif) no-repeat center left;
			}
			.flexigrid div.fbutton .copy {
				background: url(<%=request.getContextPath() %>/themes/flexigrid/images/copy.gif) no-repeat center left;
			}
		</style>

	</head>

	<body>
	<div class="bborderx">
		<table id="flex1" style="display:none"></table>
	</div>
	<!--  隐藏一个按钮 ，用来调用thickbox  -->
	<div style="display:none;">
		<input type="button" id="cssrain" href="<%=request.getContextPath() %>/article.do?action=toAdd&height=200&amp;width=300" title="新建文章专题" class="thickbox" value="aaa" />
		<input type="button" id="cssrainupdate" href="javascript:openWinrain();" title="新建文章专题" class="thickbox" value="aaa" />
	</div>	
	<div style="display:none;">
		<input type="button" id="errorrain" href="<%=request.getContextPath() %>/common/delErro.html?height=200&amp;width=300" title="出错信息！" class="thickbox" value="" />
	</div>		
		<script type="text/javascript">
			//${colmodel}
			$("#flex1").flexigrid
			(
			{
			url: '<%=request.getContextPath() %>/global.do?action=PageShow&classID=fdsa',
			dataType: 'json',
			errormsg: '连接错误！',
			colModel : ${colModel},
			/*[
				{display: '主键', name : 'id', width : 60, sortable : true, align: 'left',hide: true},
				{display: '编号', name : 'numid', width : 60, sortable : true, align: 'center'},
				{display: '专题名称', name : 'articleTpTitle', width : 120, sortable : true, align: 'center'},
				{display: '专题简介', name : 'articleTpicInstr', width : 240, sortable : true, align: 'left'},
				{display: '专题页地址', name : 'articleTpUrl', width : 120, sortable : true, align: 'left'},
				{display: '专题归属', name : 'topicType', width : 120, sortable : true, align: 'left'},
				{display: '填写时间', name : 'sys_time', width : 120, sortable : true, align: 'left'}
				],*/
			buttons : ${buttons},
				/*[
				{name: '增加', bclass: 'add', onpress : exOperation},
				{name: '修改', bclass: 'edit', onpress : exOperation},
				{name: '复制', bclass: 'copy', onpress : exOperation},
				{name: '删除', bclass: 'delete', onpress : exOperation},
				{separator: true}
				],*/
			searchitems :${searchitems}, 
			/*[
				{display: '专题名称', name : 'topic_title', isdefault: true},
				{display: '专题简介', name : 'articleTpicInstr'}
				],*/
			sortname: "id",
			sortorder: "asc",
			usepager: true,
			title: 	'${title}',//'专题管理',
			useRp: true,
			singleSelect: false,
			ifDblclick: false,
			rp: 10,
			showTableToggleBtn: true,
			width: $(window).width()-2,
			height: 400
			}
			);
			
			//操作数据
			function exOperation(com,grid){
				if (com=='删除'){
					if( $('.trSelected',grid).length>0){
						if(confirm('是否删除这 ' + $('.trSelected',grid).length + ' 条记录吗?')){
							  var  id ="";
							  //debugger;
						     for(var i=0;i<$('.trSelected',grid).length;i++){
						        id += "id="+$('.trSelected',grid).find("td:first").eq(i).text()+"&";
						     }
						      $.ajax({ 
								url: "<%=request.getContextPath()%>/article.do?action=delete" ,
								type:'post', 
								dataType:'action', 
								data: id ,  
								success:update_page
	           					}); 
	          				}
					 }else{
					   alert("请选择某行删除！");
					 }
				}else if (com=='增加'){
					$('#cssrain').trigger("click");
				}else if(com == '修改'){
				
					if( $('.trSelected',grid).length>0){
						  
						  if($('.trSelected',grid).length>0&&$('.trSelected',grid).length>1){
						  	alert("不能选择多行！请选择一行数据进行修改！");
						  }
						  
						  var  id = $('.trSelected',grid).find("td:first").eq(0).text();
						 	//windowlocation.href="<%=request.getContextPath()%>/article.do?&action=edit&id="+id+"";
						 	openWinrain(id);
						 

					 }else{
					   alert("请选择某行数据修改！");
					 }
					 
				}else if(com == '复制'){
					
				}			
			}
			
			function update_page(obj) {
				if(obj=='true'){
		         	window.location.reload();
		         }else{
		         	$('#errorrain').trigger("click");
		         }
	     	} 	
	     	//用于展示N级别数据grid
	     	function gridDblclick(){
	     		window.location.href="<%=request.getContextPath()%>/pages/admin/article.jsp?url=0000000";
	     	}
	     	function openWinrain(id){ 
	     	    	 $('#cssrainupdate').attr('href',"<%=request.getContextPath()%>/article.do?&action=edit&id="+id+"&amp;height=200&amp;width=300");
	     	 	 	$('#cssrainupdate').trigger("click");
	     	}
	</script>
	<div style="display:none;">
		<input type="button" id="editrain" href="<%=request.getContextPath() %>/common/delErro.html?height=200&amp;width=300" title="出错信息！" class="thickbox" value="" />
	</div>	
	</body>
</html>
